<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hotel extends CI_Controller {
/*---------------------Sachintha's Gallery Configuration------------*/	
	private $hotel_id 				= 0;
	private $error_no_hotel_id		= false;
	private $image_settings_arr	 	= array("upload_path" 		=> "./uploads/hotel/",
											"allowed_types"		=> 'gif|jpg|png|bmp|jpeg|GIF|JPG|PNG|JPEG',
											"img_max_width" 	=> '1000',
											"img_max_height" 	=> '1000',
											"img_thumb_width"	=> '100',
											"img_thumb_height" 	=> '100');
											
											
	public function __construct(){
		parent::__construct();
		$this->load->model('hotel_model');
	}
	
	/* created by	: sachintha
	 * purpose		: switch views according to the given parameters
	 */	
	public function gallery($hotel_id=0,$param=1,$image_id=0,$page_id=0){			
		$this->hotel_id = ($hotel_id==0 || trim($hotel_id)=="" || $hotel_id=="" )?0:$hotel_id;
		$this->error_no_hotel_id = false;	
		
		if($this->hotel_id==0 ){
			$this->hotel_not_found_redirect();
		}else{		
			switch($param){
				case '1':{ 
					$this->session->set_flashdata('selected_tab','hotel_gallery');
					//$this->gallery_image_list($page_id,$param);
					redirect("/admin/hotel/edit/".$this->hotel_id);
				}break;
				case '2':{ $this->gallery_image_add();}break;
				case '3':{ $this->gallery_image_edit($image_id);}break;			
				case '4':{ $this->gallery_image_delete($image_id);}break;
				case '5':{ $this->gallery_image_save($image_id);}break;
				default :{ $this->gallery_image_list();}break;
			}
		}
	}
	
	/* created by	: sachintha
	 * purpose		: If hotel id = 0 then redirects
	 */	
	private function hotel_not_found_redirect(){
		$data['main_content'] 		= 'hotel/hotel_not_found';
		$data['page_title']   		= "Hotel Not Found";		
		$data['message']			= get_flash_message(1);
				
		$this->load->view('admin_layout',$data);
	}
	
	/* created by	: sachintha
	 * purpose		: Adding gallery Images
	 */	
	private function gallery_image_add(){
		$data['main_content'] = "hotel/hotel_image_upload";
		$data['page_title']   = "Hotel Gallery Add";		
		$data['hotel_id']	  = $this->hotel_id;
		$data['message']  	  = "";
		$this->load->view('admin_layout',$data);
		
		
	}
		
	/* created by	: sachintha
	 * purpose		: Edit gallery images
	 */	
	private function gallery_image_edit($image_id=0){	
		$data['main_content'] = 'hotel/hotel_image_edit';	
		$data['page_title']   = "Hotel Image Edit";		
		//$date['p_hotel_id']	  = $this->hotel_id;
		$data['message']  	  = "";
		
		$this->load->model('hotel_model');
		
		if($image_id==0){
			$data['message']  	  = get_flash_message(5);
			$data['main_content'] = 'hotel/hotel_image_list';
			$data['hotel_gallery_list'] = $this->hotel_model->load_hotel_image_list($this->hotel_id);			
			$this->load->view('admin_layout',$data);
		}else{
			$data['hotel_image_data']   = $this->hotel_model->select_hotel_image($this->hotel_id,$image_id);
			
			$image_path			= "/uploads/hotel/".$data['hotel_image_data'][0]->hotel_id."/gallery/".$data['hotel_image_data'][0]->image;				
			$thumb_img_path_arr = explode('.',$data['hotel_image_data'][0]->image);			
			$thumb_img_path		= "/uploads/hotel/".$data['hotel_image_data'][0]->hotel_id."/gallery/".$thumb_img_path_arr[0]."_thumb.".$thumb_img_path_arr[1];
			
			
			if(file_exists(substr($image_path,1)) && is_file(substr($image_path,1))){
				$data['img_path']	= $image_path;									
			}else{
				$data['img_path']	='/images/not_found.gif';
			}
			
			if(file_exists(substr($thumb_img_path,1)) && is_file(substr($thumb_img_path,1))){
				$data['thumb_img']	= $thumb_img_path;									
			}else{
				$data['thumb_img']  ='/images/not_found.gif'; 
			}
			
			$this->load->view('admin_layout',$data);
		}
	}
	
	/* created by	: sachintha
	 * purpose		: Save gallery images both(add/edit)
	 */	
	private function gallery_image_save($image_id=0){		
		$data['page_title']   = "Hotel Image Save";
		$data['hotel_id']	  = $this->hotel_id;
		$data['message']  	  = "";
		$config				  = "";
		
		if($image_id==0){			
			$config['upload_path'] 		 	= $this->image_settings_arr['upload_path'].$this->hotel_id."/gallery";
			$config['allowed_types'] 	 	= $this->image_settings_arr['allowed_types'];
		}		
		$this->load->model('hotel_model');
		$this->hotel_model->save_hotel_image($this->hotel_id,$image_id,$config,$this->image_settings_arr);
	}
	
	/* created by	: sachintha
	 * purpose		: List gallery images
	 */	
	private function gallery_image_list($page_id,$param,&$data){	
		//global $data;				//die($page_id.' par '.$param);
		//$data['main_content'] = 'hotel/hotel_image_list';
		$data['page_title']   = "Hotel Gallery";
		$data['hotel_id']	  = $this->hotel_id;
		$data['message']	  = "";
		
		//$this->load->model('hotel_model');	
		$data['hotel_gallery_list'] 		= $this->hotel_model->load_hotel_image_list($this->hotel_id,$page_id);		
		$data['hotel_gallery_pagination'] 	= $this->hotel_model->load_hotel_image_pagination_list($this->hotel_id,$param,$page_id);					
		//$this->load->view('admin_layout',$data);		
	}
		
	/* created by	: sachintha
	 * purpose		: Delete gallery images
	 */	
	private function gallery_image_delete($image_id=0){		
		//$data['main_content'] = "hotel/hotel_image_list";
		//$data['page_title']   = "Hotel Gallery";
		$data['hotel_id']	  = $this->hotel_id;
		$data['message']  	  = "";
		
		$this->load->model('hotel_model');
		$rtrn_val 			  = $this->hotel_model->delete_hotel_image($this->hotel_id,$image_id,$this->image_settings_arr['upload_path']);
		
		if($rtrn_val){
			$data['message']  = get_flash_message(3);
		}else{
			$data['message']  =  get_flash_message(4);
		}
		$this->session->set_flashdata('message', $data['message']);	
		redirect("/admin/hotel/edit/".$this->hotel_id."/");
	}	
/*---------------------End Sachintha's Gallery Configuration------------*/	
	

	
	public function index(){   
		 $data['page_html_title']  = "Hotel List";
		 $data['main_content']     = 'hotel/list';
		 $data['hotel_list']       = $this->hotel_model->load_hotel_list();
		 $data['hotel_facilities'] = $this->hotel_model->load_hotel_facility_list(0);
		 $this->load->view('admin_layout',$data);
	}
	
	/*
		load a hotel to edit
	*/
	public function edit($hotel_id=0,$param=1,$image_id=0,$page_id=0){   
		$this->hotel_id=$hotel_id;
		 $data['city_list']=$this->hotel_model->load_city_list();
		 
		 $this->load->model('Hotel_Category_Model');
		 $data['hotel_category_list']=$this->Hotel_Category_Model->load_hotel_category_list();
		 
		 $data['page_html_title']   		= "Hotel Edit";
		 $data['main_content'] 		= 'hotel/edit';
		 $data['selected_hotel_id']	= $hotel_id;
		 if($hotel_id>0){
			$data['hotel_data']		  = $this->hotel_model->load_hotel_detail($hotel_id); 
		 } 
		 $data['hotel_facilities'] = $this->hotel_model->load_hotel_facility_list($hotel_id);
	/*------------------------------------*/
		//$this->hotel_id = ($hotel_id==0 || trim($hotel_id)=="" || $hotel_id=="" )?0:$hotel_id;
		$this->error_no_hotel_id = false;	
		
		if($this->hotel_id==0 ){
			$this->hotel_not_found_redirect();
		}else{
			$data['param'] 		= $param;		
			switch($param){
				case '1':{ $this->gallery_image_list($page_id,$param,$data);}break;
				case '2':{ $this->gallery_image_add();}break;
				case '3':{ $this->gallery_image_edit($image_id);}break;			
				case '4':{ $this->gallery_image_delete($image_id);}break;
				case '5':{ $this->gallery_image_save($image_id);}break;
				default :{ $this->gallery_image_list($page_id,$param,$data);}break;
			}
		}
	/*-------------------------------------*/		 
		 $this->load->view('admin_layout',$data);
	}
	
 	/*
		save a hotel
	*/	
	public function save(){    
		$this->load->library('form_validation');
		$this->form_validation->set_rules('name', 'Hotel Name', 'trim|required');
		$this->form_validation->set_rules('city_id', 'City', 'trim|required');
		$this->form_validation->set_rules('description_location', 'Description Location', 'trim|required');
		
		if($this->form_validation->run()== FALSE){
			$hotel_id 			 	 = ($_POST['hotel_id']!=''?$_POST['hotel_id']:0); 
			$data['page_html_title'] = "Hotel Save Error";
			 
		}else{
			$result=$this->hotel_model->save();
			$data['page_html_title'] = "Hotel Edit";  
			$data['message_id']		 =  $result[1];
			$hotel_id				 =  $result[0];  
		} 
		if($hotel_id>0){
			$data['hotel_data']       = $this->hotel_model->load_hotel_detail($hotel_id); 
			$data['hotel_facilities'] = $this->hotel_model->load_hotel_facility_list($hotel_id);
		}
		$data['city_list']=$this->hotel_model->load_city_list();
		
		$this->load->model('Hotel_Category_Model');
		$data['hotel_category_list']=$this->Hotel_Category_Model->load_hotel_category_list();
		 
		$data['main_content'] 	   = 'hotel/edit';
		$data['selected_hotel_id'] = $hotel_id;
		$this->load->view('admin_layout',$data); 
	}
	
 	/*
		delete a specified hotel
	*/
	public function delete($hotel_id=0){  
		 $this->load->helper('url');
		 $data['message_id']   = $this->hotel_model->delete_hotel($hotel_id);
		 redirect('/admin/hotel/', 'refresh');	
	}
}